
#include "data.h"
#include "Activity.h"
#include "global.h"
#include "statistics.h"

int no_wait_multi_experiments ( int nbJobs, int nbStages, 
						  int nbMachinesPerStage, int nbTests) {
	data md(nbJobs, nbStages, nbMachinesPerStage);
	statistics sta(nbTests);
   int nehmaxWait, maxWait, nehmakespan, makespan, alpha = 10, lbd ;
   clock_t start, end; 
   double nehduration, duration; 
   
   srand( (unsigned)time( NULL ) );
   for ( int i = 0; i < nbTests; i ++ ) {	
	   cout << "Test " << i << endl;
	   
	   init( md );
	   schedule ms(md);

	   start = clock();
	   neh(md, ms);
	   end = clock();
	   nehduration = (double)(end - start) / CLOCKS_PER_SEC;
	   
	   // Definition:
	   int m, n ;
	   deque <int> av;
	   deque <Activity> acts;
	   // Initialization:
	   for ( m = 0; m < ms.getnbSched(); m ++ ) {
		   for ( n = 0; n < ms.getnbStages(); n ++ ) {
			   Activity act(ms, m, n);
			   acts.push_back(act);	
		   }		
	   }

	   for ( m = 0; 
	   m < ms.getnbMachinesPerStage() * ms.getnbStages();
	   m ++)
		   av.push_back(0);
	   
	   nehmakespan = backtrack( ms, acts, 0, av,
		   0, 0, 0, alpha, nehmaxWait);
	   

	 
	/*
	FILE *istream;
	if(( istream  = fopen( "neh.txt", "w" )) == NULL ) {
		cout<<  "The file was not opened" << endl;
		exit(1);
	}

	for ( int u = 0; u < ms.getnbSched(); u ++) {
		int i = ms.getSched(u);
		for ( int j = 0; j < ms.getnbStages(); j ++ ) {
			fprintf(istream, "%d%s %d%s %d%s %d%s %d%s %d\n", 
				i,",",
				j,",",
				acts[i*ms.getnbStages() + j].getMachine( i, j) , ",",
				acts[i*ms.getnbStages() + j].getStime( i, j), ",", 
				acts[i*ms.getnbStages() + j].getPtime( i, j),  ",",
				acts[i*ms.getnbStages() + j].getEtime( i, j) );
		}
		fprintf(istream, "\n");
	}
	fprintf(istream, "\n");
	fprintf(istream, "%d \n", lb);
	fclose(istream);
	*/


	   lbd = lb(md);
	//   input( md, "input.txt"); // output input data to input.txt
	   
	   //use backtrack	   
	   start = clock();
	   // maxWait = fifo_partial(md, ms, lbd, alpha, makespan);
	   ts(md, ms, alpha, lbd, maxWait, makespan);
	   end = clock();
	   duration = (double)(end - start) / CLOCKS_PER_SEC;

	   sta.setLowerbound( i, lbd);

	   sta.setnehMakespan( i, nehmakespan );
	   sta.setnehMaxWait( i, nehmaxWait);
	   sta.setnehTimes( i, nehduration);

	   sta.setMakespan( i, makespan );
	   sta.setMaxWait( i, maxWait); 
	   sta.setTimes( i, duration);
	   
   }
   
   FILE *istream;
   if(( istream  = fopen( "no_wait_statistics.txt", "w" )) == NULL ) {
	   cout<<  "The file was not opened" << endl;
	   exit(1);
   }
   
   fprintf(istream, "%d %d %d %d\n", nbJobs, nbStages, 
	   nbMachinesPerStage, nbTests);
   for ( i = 0; i < nbTests; i ++) {
	   fprintf(istream, "%d %d %d %d %f %d  %d  %f \n", 
		   i, 
		   sta.getLowerbound(i),

		   sta.getnehMakespan(i),
		   sta.getnehMaxWait(i),
		   sta.getnehTimes(i),

		   sta.getMakespan(i),		    
		   sta.getMaxWait(i),
		   sta.getTimes(i));
   }
   fprintf(istream, "\n");
   fclose(istream);
   

   return 0;
}


